cmake_minimum_required(VERSION 3.10)
project(ChatServer)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置输出目录为项目同级目录下的 bin 文件夹
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 查找必要的库
find_package(Threads REQUIRED)

# 如果是 Windows 平台，需要定义 Winsock 库
if(WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_WIN32_WINNT=0x0601")
endif()

# 添加可执行文件
add_executable(ChatServer server.cpp)

# 创建专门的资源目录（推荐在项目根目录创建 resources 文件夹）
set(RESOURCES_DIR "${CMAKE_SOURCE_DIR}/resources")

# 包含头文件目录
target_include_directories(ChatServer PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 链接必要的库
target_link_libraries(ChatServer PRIVATE
    Threads::Threads
)

# 在 Windows 上链接 Winsock 库
if(WIN32)
    target_link_libraries(ChatServer PRIVATE ws2_32)
endif()

add_custom_command(TARGET ChatServer POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
    "${CMAKE_SOURCE_DIR}/index.html"
    "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/index.html"
    COMMENT "复制 index.html 到输出目录"
)

add_custom_command(TARGET ChatServer POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
    "${CMAKE_SOURCE_DIR}/style.css"
    "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/style.css"
    COMMENT "复制 style.css 到输出目录"
)

# 确保输出目录存在
file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
